<?php

declare(strict_types=1);

namespace App\Controller\delivery;
use App\Controller\BaseController;
use App\Model\DataBackhaulSetting;
use App\Model\RechargeTemplate;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Model\DataSpread;
use App\Common\Response\SystemCode;
use App\Service\delivery\DataSpreadService;

#[AutoController]
class DataSpreadController  extends BaseController
{

    #[Inject]
    private DataSpreadService $dataSpreadService;
    /**
     * 推广链接列表
     */
    public function Index(RequestInterface $request)
    {

        $data = $this->dataSpreadService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }
    /**
     * 推广链接详情
     */
    public function View(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $dataSpreadInfo = DataSpread::query()->select('id', 'name', 'status')->where('id', $id)->first();
            if (empty($dataSpreadInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '推广链接不存在');
            }
            $appletDataSpreadInfo = DataSpread::query()->where(['DataSpread_id' => $dataSpreadInfo->id])->get();
            $appletName = '';

            foreach ($appletDataSpreadInfo as $appletDataSpread){
                $appletName .= $appletDataSpread->applet->name.',';
            }

            $dataSpreadInfo->applet_name = substr($appletName,0,-1);
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $dataSpreadInfo);
        }
    }
    /**
     * 推广链接创建
     */
    public function Create(RequestInterface $request){

        $result = $this->dataSpreadService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response($result['code'], $result['message']);

        }
    }
    /**
     * 推广链接删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $dataSpreadInfo = DataSpread::query()->where('id', $id)->first();
            if (empty($dataSpreadInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '推广链接不存在');
            }
            $dataSpreadInfo->deleted_at = date('Y-m-d H:i:s');
            $dataSpreadInfo->save();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
        }
    }
    /**
     * 推广链接更新
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $dataSpreadInfo = DataSpread::query()->select(['id','company_id','applet_id','retention_template_id','assistant_retention_template_id','playlet_id','assistant_recharge_template_id','recharge_template_id','spread_platform_id','playlet_name',
                'playlet_number','ad_account','backhaul_setting_id','playlet_number_num','operation_mode','playlet_number_num_pay','recharge_template_type','name','playlet_number_name','user_type','user_id','wx_link','wx_route','route_key','dy_link'
            ])->where(['id' => $id])->first();
            if(empty($dataSpreadInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '推广链接不存在');
            }
            if($request->getMethod() == 'GET'){

                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$dataSpreadInfo);

            }else{
                //$dataSpreadInfo = DataSpread::query()->where(['id' => $id])->first();
                $result = $this->dataSpreadService->createOrUpdate($request->post(),true,$dataSpreadInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response($result['code'], $result['message']);

                }
            }
        }
    }


    public function monitoringLink(int $id,RequestInterface $request){
        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $dataSpreadInfo = DataSpread::query()->select(['id','spread_platform_id','backhaul_setting_id','url_toutiao10','url_toutiao20','operation_mode'])->where(['id' => $id])->first();
            if(empty($dataSpreadInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '推广链接不存在');
            }
            if($request->getMethod() == 'GET'){

                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$dataSpreadInfo);

            }else{
                $backhaul_setting_id = $request->post('backhaul_setting_id') ?? 0;

                //查找短剧是否存在
                if((int)$backhaul_setting_id != 0){
                    $dataBackhaulSettingData = DataBackhaulSetting::query()->where(['id' => $backhaul_setting_id])->select(['id','name','operation_mode'])->first();
                    if(empty($dataBackhaulSettingData)){
                        return $this->response(SystemCode::SYSTEM_ERROR, '回传监测配置不存在');
                    }
                    //判断IAA模式，选择的
                    if($dataSpreadInfo->operation_mode != $dataBackhaulSettingData->operation_mode){
                        if($dataSpreadInfo->operation_mode == 2){
                            return $this->response(SystemCode::SYSTEM_ERROR, '回传监测配置的模式需为IAA');
                        }
                        if($dataSpreadInfo->operation_mode == 1){
                            return $this->response(SystemCode::SYSTEM_ERROR, '回传监测配置的模式需为付费模式');
                        }
                    }
                }


                $dataSpreadInfo->backhaul_setting_id = $backhaul_setting_id;
                if($dataSpreadInfo->save()){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, '修改错误');

                }
            }
        }
    }

    public function PromotionLink(int $id,RequestInterface $request){
        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $dataSpreadInfo = DataSpread::query()->select(['id','wx_link','wx_route','dy_link','dy_page','dy_parameter','dy_route'])->where(['id' => $id])->first();
            if(empty($dataSpreadInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '推广链接不存在');
            }

            $linkData = explode("?", $dataSpreadInfo->wx_link);
            $dataSpreadInfo->wx_link_private = $linkData[0].'.html?'.$linkData[1];
            $dataSpreadInfo->dy_link = '暂且为空';

            return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$dataSpreadInfo);
        }
    }


    public function getTemplete(int $type = 1){

        $userInfo = $this->dataSpreadService->getUserAppletInfo(2);
        $company_id = $userInfo['company_id'];
        $user_id = $userInfo['id'];
        $query = RechargeTemplate::query()->select(['id as value','name as label']);

        if($userInfo['role']['is_admin'] != 1){
            $query->where('user_id',$userInfo['id']);
        }
        $rechargeTemplateData = $query->where(['company_id' => $company_id,'type' => $type])->get();
        if(empty($rechargeTemplateData)){
            return $this->response(SystemCode::SYSTEM_ERROR, '充值模板不存在,或者模板不属于该小程序');
        }else{
            return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$rechargeTemplateData);
        }

    }

    public function batchSettings(RequestInterface $request){

        $result = $this->dataSpreadService->batchSettings($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '上传成功');
        }else{
            return $this->response($result['code'], $result['message'],$result['data']);

        }
    }

    public function deliverySettings(int $applet_id){
        $result = $this->dataSpreadService->deliverybackhaulSetting($applet_id);
        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '获取成功',$result['data']);
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }
    }

    public function Copy(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $dataSpreadInfo = DataSpread::query()->where('id', $id)->first();
            if (empty($dataSpreadInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '推广链接不存在');
            }
            $result = $this->dataSpreadService->copy($dataSpreadInfo);

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '复制成功');
            }else{
                return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

            }
        }
    }

    public function accountBind(int $id,int $type = 0){

        if($type == 1){
            $dataSpreadInfo = DataSpread::query()->select(['id','ad_account'])->where(['ad_account' => $id])->first();
        }else{
            $dataSpreadInfo = DataSpread::query()->select(['id','ad_account'])->where(['id' => $id])->first();
        }
        if(empty($dataSpreadInfo)){
            return $this->response(SystemCode::SYSTEM_ERROR, '推广链接不存在');
        }

        $result = $this->dataSpreadService->accountBind($dataSpreadInfo);

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '解绑成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }

    }
}